{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_and_process():\n",
    "    result = pd.read_csv('./data/nba-player-2014.csv', na_values='-')\n",
    "    result.fillna(0, inplace=True)\n",
    "    return result\n",
    "\n",
    "\n",
    "def filter_columns(dataframe, excluded):\n",
    "    return dataframe[list(set(dataframe.columns) - set(excluded))]\n",
    "\n",
    "\n",
    "def normalize(dataframe):\n",
    "    return (dataframe - dataframe.mean()) / dataframe.std()\n",
    "\n",
    "\n",
    "def calculate_distance(s1, s2):\n",
    "    return np.sqrt(np.power(s1 - s2, 2).sum(axis=1))\n",
    "\n",
    "\n",
    "def kmeans(stat, k, iterations):\n",
    "    centers = stat.sample(n=k)\n",
    "    for i in range(iterations):\n",
    "        centers.reset_index(drop=True, inplace=True)\n",
    "        if i > 0 and i % 10 == 0:\n",
    "            print('iteration round', i)\n",
    "        distance_matrix = centers.apply(lambda c: calculate_distance(stat, c), axis=1)\n",
    "        belongings = distance_matrix.idxmin()\n",
    "        centers = stat.groupby(belongings).mean()\n",
    "    return belongings\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iteration round 10\n",
      "iteration round 20\n",
      "iteration round 30\n",
      "iteration round 40\n",
      "iteration round 50\n",
      "iteration round 60\n",
      "iteration round 70\n",
      "iteration round 80\n",
      "iteration round 90\n"
     ]
    }
   ],
   "source": [
    "distance_excluded_columns = ['fullname']\n",
    "players = load_and_process()\n",
    "normalized_stat = normalize(filter_columns(players, distance_excluded_columns))\n",
    "\n",
    "labels = kmeans(normalized_stat, 5, 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    28\n",
       "1    38\n",
       "2    67\n",
       "3    88\n",
       "4    46\n",
       "dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players.groupby(labels).count().max(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>FTA/G</th>\n",
       "      <th>MPG</th>\n",
       "      <th>FT%</th>\n",
       "      <th>PPG</th>\n",
       "      <th>GP</th>\n",
       "      <th>FGM/G</th>\n",
       "      <th>3FGA/G</th>\n",
       "      <th>3FG%</th>\n",
       "      <th>HIGH</th>\n",
       "      <th>PPS</th>\n",
       "      <th>3FGM/G</th>\n",
       "      <th>FTM/G</th>\n",
       "      <th>FG%</th>\n",
       "      <th>PTS/48</th>\n",
       "      <th>GS</th>\n",
       "      <th>fullname</th>\n",
       "      <th>PTS</th>\n",
       "      <th>FGA/G</th>\n",
       "      <th>Group</th>\n",
       "      <th>Distance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7.7</td>\n",
       "      <td>36.1</td>\n",
       "      <td>0.710</td>\n",
       "      <td>25.3</td>\n",
       "      <td>69</td>\n",
       "      <td>9.0</td>\n",
       "      <td>4.9</td>\n",
       "      <td>0.354</td>\n",
       "      <td>42</td>\n",
       "      <td>1.36</td>\n",
       "      <td>1.7</td>\n",
       "      <td>5.4</td>\n",
       "      <td>0.488</td>\n",
       "      <td>33.6</td>\n",
       "      <td>69</td>\n",
       "      <td>James, LeBron</td>\n",
       "      <td>1743</td>\n",
       "      <td>18.5</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5.8</td>\n",
       "      <td>35.4</td>\n",
       "      <td>0.858</td>\n",
       "      <td>21.1</td>\n",
       "      <td>68</td>\n",
       "      <td>7.5</td>\n",
       "      <td>3.2</td>\n",
       "      <td>0.359</td>\n",
       "      <td>40</td>\n",
       "      <td>1.28</td>\n",
       "      <td>1.2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.455</td>\n",
       "      <td>28.5</td>\n",
       "      <td>67</td>\n",
       "      <td>Gay, Rudy</td>\n",
       "      <td>1432</td>\n",
       "      <td>16.4</td>\n",
       "      <td>0</td>\n",
       "      <td>2.928869</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>6.1</td>\n",
       "      <td>34.4</td>\n",
       "      <td>0.812</td>\n",
       "      <td>19.3</td>\n",
       "      <td>76</td>\n",
       "      <td>6.4</td>\n",
       "      <td>4.3</td>\n",
       "      <td>0.364</td>\n",
       "      <td>33</td>\n",
       "      <td>1.35</td>\n",
       "      <td>1.6</td>\n",
       "      <td>4.9</td>\n",
       "      <td>0.445</td>\n",
       "      <td>26.8</td>\n",
       "      <td>76</td>\n",
       "      <td>Hayward, Gordon</td>\n",
       "      <td>1463</td>\n",
       "      <td>14.3</td>\n",
       "      <td>0</td>\n",
       "      <td>3.423804</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>6.0</td>\n",
       "      <td>31.8</td>\n",
       "      <td>0.768</td>\n",
       "      <td>21.5</td>\n",
       "      <td>62</td>\n",
       "      <td>8.2</td>\n",
       "      <td>1.6</td>\n",
       "      <td>0.284</td>\n",
       "      <td>42</td>\n",
       "      <td>1.23</td>\n",
       "      <td>0.5</td>\n",
       "      <td>4.6</td>\n",
       "      <td>0.470</td>\n",
       "      <td>32.4</td>\n",
       "      <td>62</td>\n",
       "      <td>Wade, Dwyane</td>\n",
       "      <td>1331</td>\n",
       "      <td>17.5</td>\n",
       "      <td>0</td>\n",
       "      <td>3.485929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>4.9</td>\n",
       "      <td>36.4</td>\n",
       "      <td>0.863</td>\n",
       "      <td>21.7</td>\n",
       "      <td>75</td>\n",
       "      <td>7.7</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.415</td>\n",
       "      <td>57</td>\n",
       "      <td>1.32</td>\n",
       "      <td>2.1</td>\n",
       "      <td>4.2</td>\n",
       "      <td>0.468</td>\n",
       "      <td>28.6</td>\n",
       "      <td>75</td>\n",
       "      <td>Irving, Kyrie</td>\n",
       "      <td>1628</td>\n",
       "      <td>16.5</td>\n",
       "      <td>0</td>\n",
       "      <td>3.555443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>7.1</td>\n",
       "      <td>38.7</td>\n",
       "      <td>0.834</td>\n",
       "      <td>20.0</td>\n",
       "      <td>65</td>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.378</td>\n",
       "      <td>35</td>\n",
       "      <td>1.43</td>\n",
       "      <td>1.1</td>\n",
       "      <td>5.9</td>\n",
       "      <td>0.462</td>\n",
       "      <td>24.8</td>\n",
       "      <td>65</td>\n",
       "      <td>Butler, Jimmy</td>\n",
       "      <td>1301</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3.649827</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5.1</td>\n",
       "      <td>35.4</td>\n",
       "      <td>0.845</td>\n",
       "      <td>23.4</td>\n",
       "      <td>71</td>\n",
       "      <td>9.3</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.352</td>\n",
       "      <td>39</td>\n",
       "      <td>1.17</td>\n",
       "      <td>0.5</td>\n",
       "      <td>4.3</td>\n",
       "      <td>0.466</td>\n",
       "      <td>31.7</td>\n",
       "      <td>71</td>\n",
       "      <td>Aldridge, LaMarcus</td>\n",
       "      <td>1661</td>\n",
       "      <td>19.9</td>\n",
       "      <td>0</td>\n",
       "      <td>3.667809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6.4</td>\n",
       "      <td>35.2</td>\n",
       "      <td>0.728</td>\n",
       "      <td>21.9</td>\n",
       "      <td>67</td>\n",
       "      <td>8.6</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.400</td>\n",
       "      <td>45</td>\n",
       "      <td>1.28</td>\n",
       "      <td>0.1</td>\n",
       "      <td>4.6</td>\n",
       "      <td>0.502</td>\n",
       "      <td>29.9</td>\n",
       "      <td>67</td>\n",
       "      <td>Griffin, Blake</td>\n",
       "      <td>1469</td>\n",
       "      <td>17.1</td>\n",
       "      <td>0</td>\n",
       "      <td>3.685176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9.8</td>\n",
       "      <td>34.4</td>\n",
       "      <td>0.835</td>\n",
       "      <td>28.1</td>\n",
       "      <td>67</td>\n",
       "      <td>9.4</td>\n",
       "      <td>4.3</td>\n",
       "      <td>0.299</td>\n",
       "      <td>54</td>\n",
       "      <td>1.28</td>\n",
       "      <td>1.3</td>\n",
       "      <td>8.1</td>\n",
       "      <td>0.426</td>\n",
       "      <td>39.3</td>\n",
       "      <td>67</td>\n",
       "      <td>Westbrook, Russell</td>\n",
       "      <td>1886</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3.722021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>4.9</td>\n",
       "      <td>35.7</td>\n",
       "      <td>0.864</td>\n",
       "      <td>21.0</td>\n",
       "      <td>82</td>\n",
       "      <td>7.2</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.343</td>\n",
       "      <td>43</td>\n",
       "      <td>1.26</td>\n",
       "      <td>2.4</td>\n",
       "      <td>4.2</td>\n",
       "      <td>0.434</td>\n",
       "      <td>28.2</td>\n",
       "      <td>82</td>\n",
       "      <td>Lillard, Damian</td>\n",
       "      <td>1720</td>\n",
       "      <td>16.6</td>\n",
       "      <td>0</td>\n",
       "      <td>3.967624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6.8</td>\n",
       "      <td>36.1</td>\n",
       "      <td>0.805</td>\n",
       "      <td>24.4</td>\n",
       "      <td>68</td>\n",
       "      <td>9.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.083</td>\n",
       "      <td>43</td>\n",
       "      <td>1.38</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.5</td>\n",
       "      <td>0.535</td>\n",
       "      <td>32.4</td>\n",
       "      <td>68</td>\n",
       "      <td>Davis, Anthony</td>\n",
       "      <td>1656</td>\n",
       "      <td>17.6</td>\n",
       "      <td>0</td>\n",
       "      <td>4.088982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9.2</td>\n",
       "      <td>34.1</td>\n",
       "      <td>0.782</td>\n",
       "      <td>24.1</td>\n",
       "      <td>59</td>\n",
       "      <td>8.4</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.250</td>\n",
       "      <td>39</td>\n",
       "      <td>1.33</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.2</td>\n",
       "      <td>0.467</td>\n",
       "      <td>33.9</td>\n",
       "      <td>59</td>\n",
       "      <td>Cousins, DeMarcus</td>\n",
       "      <td>1421</td>\n",
       "      <td>18.1</td>\n",
       "      <td>0</td>\n",
       "      <td>4.297432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>7.2</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0.832</td>\n",
       "      <td>20.1</td>\n",
       "      <td>60</td>\n",
       "      <td>6.8</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.284</td>\n",
       "      <td>42</td>\n",
       "      <td>1.22</td>\n",
       "      <td>0.4</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.413</td>\n",
       "      <td>27.5</td>\n",
       "      <td>60</td>\n",
       "      <td>DeRozan, DeMar</td>\n",
       "      <td>1204</td>\n",
       "      <td>16.5</td>\n",
       "      <td>0</td>\n",
       "      <td>4.327920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>4.4</td>\n",
       "      <td>35.7</td>\n",
       "      <td>0.819</td>\n",
       "      <td>17.9</td>\n",
       "      <td>72</td>\n",
       "      <td>6.6</td>\n",
       "      <td>3.4</td>\n",
       "      <td>0.339</td>\n",
       "      <td>38</td>\n",
       "      <td>1.19</td>\n",
       "      <td>1.2</td>\n",
       "      <td>3.6</td>\n",
       "      <td>0.436</td>\n",
       "      <td>24.1</td>\n",
       "      <td>71</td>\n",
       "      <td>Oladipo, Victor</td>\n",
       "      <td>1292</td>\n",
       "      <td>15.1</td>\n",
       "      <td>0</td>\n",
       "      <td>4.499264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>3.9</td>\n",
       "      <td>34.8</td>\n",
       "      <td>0.900</td>\n",
       "      <td>19.1</td>\n",
       "      <td>82</td>\n",
       "      <td>6.9</td>\n",
       "      <td>4.3</td>\n",
       "      <td>0.398</td>\n",
       "      <td>41</td>\n",
       "      <td>1.34</td>\n",
       "      <td>1.7</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.485</td>\n",
       "      <td>26.3</td>\n",
       "      <td>82</td>\n",
       "      <td>Paul, Chris</td>\n",
       "      <td>1564</td>\n",
       "      <td>14.3</td>\n",
       "      <td>0</td>\n",
       "      <td>4.502929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>4.5</td>\n",
       "      <td>34.5</td>\n",
       "      <td>0.808</td>\n",
       "      <td>17.8</td>\n",
       "      <td>70</td>\n",
       "      <td>6.1</td>\n",
       "      <td>5.6</td>\n",
       "      <td>0.338</td>\n",
       "      <td>39</td>\n",
       "      <td>1.19</td>\n",
       "      <td>1.9</td>\n",
       "      <td>3.6</td>\n",
       "      <td>0.412</td>\n",
       "      <td>24.7</td>\n",
       "      <td>70</td>\n",
       "      <td>Lowry, Kyle</td>\n",
       "      <td>1244</td>\n",
       "      <td>14.9</td>\n",
       "      <td>0</td>\n",
       "      <td>4.541434</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>5.4</td>\n",
       "      <td>34.6</td>\n",
       "      <td>0.800</td>\n",
       "      <td>17.0</td>\n",
       "      <td>81</td>\n",
       "      <td>5.8</td>\n",
       "      <td>3.4</td>\n",
       "      <td>0.324</td>\n",
       "      <td>34</td>\n",
       "      <td>1.32</td>\n",
       "      <td>1.1</td>\n",
       "      <td>4.4</td>\n",
       "      <td>0.447</td>\n",
       "      <td>23.6</td>\n",
       "      <td>81</td>\n",
       "      <td>Bledsoe, Eric</td>\n",
       "      <td>1377</td>\n",
       "      <td>12.9</td>\n",
       "      <td>0</td>\n",
       "      <td>4.626776</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.2</td>\n",
       "      <td>36.8</td>\n",
       "      <td>0.868</td>\n",
       "      <td>27.4</td>\n",
       "      <td>81</td>\n",
       "      <td>8.0</td>\n",
       "      <td>6.9</td>\n",
       "      <td>0.375</td>\n",
       "      <td>51</td>\n",
       "      <td>1.51</td>\n",
       "      <td>2.6</td>\n",
       "      <td>8.8</td>\n",
       "      <td>0.440</td>\n",
       "      <td>35.7</td>\n",
       "      <td>81</td>\n",
       "      <td>Harden, James</td>\n",
       "      <td>2217</td>\n",
       "      <td>18.1</td>\n",
       "      <td>0</td>\n",
       "      <td>4.681894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>3.8</td>\n",
       "      <td>33.7</td>\n",
       "      <td>0.752</td>\n",
       "      <td>18.9</td>\n",
       "      <td>80</td>\n",
       "      <td>7.5</td>\n",
       "      <td>3.6</td>\n",
       "      <td>0.285</td>\n",
       "      <td>38</td>\n",
       "      <td>1.12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.9</td>\n",
       "      <td>0.445</td>\n",
       "      <td>26.9</td>\n",
       "      <td>80</td>\n",
       "      <td>Ellis, Monta</td>\n",
       "      <td>1513</td>\n",
       "      <td>16.9</td>\n",
       "      <td>0</td>\n",
       "      <td>4.712634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>3.3</td>\n",
       "      <td>31.9</td>\n",
       "      <td>0.879</td>\n",
       "      <td>21.7</td>\n",
       "      <td>77</td>\n",
       "      <td>7.8</td>\n",
       "      <td>7.1</td>\n",
       "      <td>0.439</td>\n",
       "      <td>52</td>\n",
       "      <td>1.28</td>\n",
       "      <td>3.1</td>\n",
       "      <td>2.9</td>\n",
       "      <td>0.463</td>\n",
       "      <td>32.6</td>\n",
       "      <td>77</td>\n",
       "      <td>Thompson, Klay</td>\n",
       "      <td>1668</td>\n",
       "      <td>16.9</td>\n",
       "      <td>0</td>\n",
       "      <td>4.834636</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>4.2</td>\n",
       "      <td>32.7</td>\n",
       "      <td>0.914</td>\n",
       "      <td>23.8</td>\n",
       "      <td>80</td>\n",
       "      <td>8.2</td>\n",
       "      <td>8.1</td>\n",
       "      <td>0.443</td>\n",
       "      <td>51</td>\n",
       "      <td>1.42</td>\n",
       "      <td>3.6</td>\n",
       "      <td>3.9</td>\n",
       "      <td>0.487</td>\n",
       "      <td>34.9</td>\n",
       "      <td>80</td>\n",
       "      <td>Curry, Stephen</td>\n",
       "      <td>1900</td>\n",
       "      <td>16.8</td>\n",
       "      <td>0</td>\n",
       "      <td>4.887398</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>4.7</td>\n",
       "      <td>34.4</td>\n",
       "      <td>0.803</td>\n",
       "      <td>18.5</td>\n",
       "      <td>78</td>\n",
       "      <td>7.3</td>\n",
       "      <td>0.3</td>\n",
       "      <td>0.462</td>\n",
       "      <td>46</td>\n",
       "      <td>1.25</td>\n",
       "      <td>0.2</td>\n",
       "      <td>3.8</td>\n",
       "      <td>0.494</td>\n",
       "      <td>25.9</td>\n",
       "      <td>78</td>\n",
       "      <td>Gasol, Pau</td>\n",
       "      <td>1446</td>\n",
       "      <td>14.8</td>\n",
       "      <td>0</td>\n",
       "      <td>4.989331</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>3.8</td>\n",
       "      <td>29.6</td>\n",
       "      <td>0.882</td>\n",
       "      <td>17.3</td>\n",
       "      <td>77</td>\n",
       "      <td>6.3</td>\n",
       "      <td>3.6</td>\n",
       "      <td>0.380</td>\n",
       "      <td>30</td>\n",
       "      <td>1.26</td>\n",
       "      <td>1.4</td>\n",
       "      <td>3.3</td>\n",
       "      <td>0.459</td>\n",
       "      <td>28.0</td>\n",
       "      <td>77</td>\n",
       "      <td>Nowitzki, Dirk</td>\n",
       "      <td>1333</td>\n",
       "      <td>13.8</td>\n",
       "      <td>0</td>\n",
       "      <td>5.007452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>4.3</td>\n",
       "      <td>33.8</td>\n",
       "      <td>0.804</td>\n",
       "      <td>16.4</td>\n",
       "      <td>75</td>\n",
       "      <td>5.5</td>\n",
       "      <td>5.2</td>\n",
       "      <td>0.367</td>\n",
       "      <td>32</td>\n",
       "      <td>1.29</td>\n",
       "      <td>1.9</td>\n",
       "      <td>3.4</td>\n",
       "      <td>0.434</td>\n",
       "      <td>23.3</td>\n",
       "      <td>75</td>\n",
       "      <td>Love, Kevin</td>\n",
       "      <td>1228</td>\n",
       "      <td>12.7</td>\n",
       "      <td>0</td>\n",
       "      <td>5.064724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>4.6</td>\n",
       "      <td>34.2</td>\n",
       "      <td>0.827</td>\n",
       "      <td>17.3</td>\n",
       "      <td>62</td>\n",
       "      <td>6.1</td>\n",
       "      <td>4.5</td>\n",
       "      <td>0.304</td>\n",
       "      <td>42</td>\n",
       "      <td>1.10</td>\n",
       "      <td>1.4</td>\n",
       "      <td>3.8</td>\n",
       "      <td>0.385</td>\n",
       "      <td>24.4</td>\n",
       "      <td>58</td>\n",
       "      <td>Walker, Kemba</td>\n",
       "      <td>1075</td>\n",
       "      <td>15.8</td>\n",
       "      <td>0</td>\n",
       "      <td>5.170208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>3.5</td>\n",
       "      <td>32.3</td>\n",
       "      <td>0.874</td>\n",
       "      <td>17.0</td>\n",
       "      <td>63</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>0.389</td>\n",
       "      <td>34</td>\n",
       "      <td>1.20</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.1</td>\n",
       "      <td>0.422</td>\n",
       "      <td>25.2</td>\n",
       "      <td>61</td>\n",
       "      <td>Knight, Brandon</td>\n",
       "      <td>1070</td>\n",
       "      <td>14.1</td>\n",
       "      <td>0</td>\n",
       "      <td>5.428664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>5.2</td>\n",
       "      <td>25.8</td>\n",
       "      <td>0.868</td>\n",
       "      <td>16.4</td>\n",
       "      <td>67</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>0.373</td>\n",
       "      <td>34</td>\n",
       "      <td>1.38</td>\n",
       "      <td>1.9</td>\n",
       "      <td>4.5</td>\n",
       "      <td>0.421</td>\n",
       "      <td>30.6</td>\n",
       "      <td>1</td>\n",
       "      <td>Thomas, Isaiah</td>\n",
       "      <td>1101</td>\n",
       "      <td>11.9</td>\n",
       "      <td>0</td>\n",
       "      <td>5.456258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>4.9</td>\n",
       "      <td>25.2</td>\n",
       "      <td>0.861</td>\n",
       "      <td>15.5</td>\n",
       "      <td>80</td>\n",
       "      <td>4.7</td>\n",
       "      <td>5.6</td>\n",
       "      <td>0.340</td>\n",
       "      <td>36</td>\n",
       "      <td>1.34</td>\n",
       "      <td>1.9</td>\n",
       "      <td>4.3</td>\n",
       "      <td>0.404</td>\n",
       "      <td>29.6</td>\n",
       "      <td>0</td>\n",
       "      <td>Williams, Lou</td>\n",
       "      <td>1242</td>\n",
       "      <td>11.6</td>\n",
       "      <td>0</td>\n",
       "      <td>5.865021</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    FTA/G   MPG    FT%   PPG  GP  FGM/G  3FGA/G   3FG%  HIGH   PPS  3FGM/G  \\\n",
       "2     7.7  36.1  0.710  25.3  69    9.0     4.9  0.354    42  1.36     1.7   \n",
       "11    5.8  35.4  0.858  21.1  68    7.5     3.2  0.359    40  1.28     1.2   \n",
       "15    6.1  34.4  0.812  19.3  76    6.4     4.3  0.364    33  1.35     1.6   \n",
       "10    6.0  31.8  0.768  21.5  62    8.2     1.6  0.284    42  1.23     0.5   \n",
       "8     4.9  36.4  0.863  21.7  75    7.7     5.0  0.415    57  1.32     2.1   \n",
       "14    7.1  38.7  0.834  20.0  65    6.5     3.0  0.378    35  1.43     1.1   \n",
       "6     5.1  35.4  0.845  23.4  71    9.3     1.5  0.352    39  1.17     0.5   \n",
       "7     6.4  35.2  0.728  21.9  67    8.6     0.4  0.400    45  1.28     0.1   \n",
       "0     9.8  34.4  0.835  28.1  67    9.4     4.3  0.299    54  1.28     1.3   \n",
       "12    4.9  35.7  0.864  21.0  82    7.2     7.0  0.343    43  1.26     2.4   \n",
       "3     6.8  36.1  0.805  24.4  68    9.4     0.2  0.083    43  1.38     0.0   \n",
       "4     9.2  34.1  0.782  24.1  59    8.4     0.1  0.250    39  1.33     0.0   \n",
       "13    7.2  35.0  0.832  20.1  60    6.8     1.5  0.284    42  1.22     0.4   \n",
       "20    4.4  35.7  0.819  17.9  72    6.6     3.4  0.339    38  1.19     1.2   \n",
       "17    3.9  34.8  0.900  19.1  82    6.9     4.3  0.398    41  1.34     1.7   \n",
       "21    4.5  34.5  0.808  17.8  70    6.1     5.6  0.338    39  1.19     1.9   \n",
       "28    5.4  34.6  0.800  17.0  81    5.8     3.4  0.324    34  1.32     1.1   \n",
       "1    10.2  36.8  0.868  27.4  81    8.0     6.9  0.375    51  1.51     2.6   \n",
       "18    3.8  33.7  0.752  18.9  80    7.5     3.6  0.285    38  1.12     1.0   \n",
       "9     3.3  31.9  0.879  21.7  77    7.8     7.1  0.439    52  1.28     3.1   \n",
       "5     4.2  32.7  0.914  23.8  80    8.2     8.1  0.443    51  1.42     3.6   \n",
       "19    4.7  34.4  0.803  18.5  78    7.3     0.3  0.462    46  1.25     0.2   \n",
       "24    3.8  29.6  0.882  17.3  77    6.3     3.6  0.380    30  1.26     1.4   \n",
       "36    4.3  33.8  0.804  16.4  75    5.5     5.2  0.367    32  1.29     1.9   \n",
       "25    4.6  34.2  0.827  17.3  62    6.1     4.5  0.304    42  1.10     1.4   \n",
       "29    3.5  32.3  0.874  17.0  63    6.0     5.1  0.389    34  1.20     2.0   \n",
       "37    5.2  25.8  0.868  16.4  67    5.0     5.2  0.373    34  1.38     1.9   \n",
       "47    4.9  25.2  0.861  15.5  80    4.7     5.6  0.340    36  1.34     1.9   \n",
       "\n",
       "    FTM/G    FG%  PTS/48  GS            fullname   PTS  FGA/G  Group  Distance  \n",
       "2     5.4  0.488    33.6  69       James, LeBron  1743   18.5      0  0.000000  \n",
       "11    5.0  0.455    28.5  67           Gay, Rudy  1432   16.4      0  2.928869  \n",
       "15    4.9  0.445    26.8  76     Hayward, Gordon  1463   14.3      0  3.423804  \n",
       "10    4.6  0.470    32.4  62        Wade, Dwyane  1331   17.5      0  3.485929  \n",
       "8     4.2  0.468    28.6  75       Irving, Kyrie  1628   16.5      0  3.555443  \n",
       "14    5.9  0.462    24.8  65       Butler, Jimmy  1301   14.0      0  3.649827  \n",
       "6     4.3  0.466    31.7  71  Aldridge, LaMarcus  1661   19.9      0  3.667809  \n",
       "7     4.6  0.502    29.9  67      Griffin, Blake  1469   17.1      0  3.685176  \n",
       "0     8.1  0.426    39.3  67  Westbrook, Russell  1886   22.0      0  3.722021  \n",
       "12    4.2  0.434    28.2  82     Lillard, Damian  1720   16.6      0  3.967624  \n",
       "3     5.5  0.535    32.4  68      Davis, Anthony  1656   17.6      0  4.088982  \n",
       "4     7.2  0.467    33.9  59   Cousins, DeMarcus  1421   18.1      0  4.297432  \n",
       "13    6.0  0.413    27.5  60      DeRozan, DeMar  1204   16.5      0  4.327920  \n",
       "20    3.6  0.436    24.1  71     Oladipo, Victor  1292   15.1      0  4.499264  \n",
       "17    3.5  0.485    26.3  82         Paul, Chris  1564   14.3      0  4.502929  \n",
       "21    3.6  0.412    24.7  70         Lowry, Kyle  1244   14.9      0  4.541434  \n",
       "28    4.4  0.447    23.6  81       Bledsoe, Eric  1377   12.9      0  4.626776  \n",
       "1     8.8  0.440    35.7  81       Harden, James  2217   18.1      0  4.681894  \n",
       "18    2.9  0.445    26.9  80        Ellis, Monta  1513   16.9      0  4.712634  \n",
       "9     2.9  0.463    32.6  77      Thompson, Klay  1668   16.9      0  4.834636  \n",
       "5     3.9  0.487    34.9  80      Curry, Stephen  1900   16.8      0  4.887398  \n",
       "19    3.8  0.494    25.9  78          Gasol, Pau  1446   14.8      0  4.989331  \n",
       "24    3.3  0.459    28.0  77      Nowitzki, Dirk  1333   13.8      0  5.007452  \n",
       "36    3.4  0.434    23.3  75         Love, Kevin  1228   12.7      0  5.064724  \n",
       "25    3.8  0.385    24.4  58       Walker, Kemba  1075   15.8      0  5.170208  \n",
       "29    3.1  0.422    25.2  61     Knight, Brandon  1070   14.1      0  5.428664  \n",
       "37    4.5  0.421    30.6   1      Thomas, Isaiah  1101   11.9      0  5.456258  \n",
       "47    4.3  0.404    29.6   0       Williams, Lou  1242   11.6      0  5.865021  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players['Group'] = labels\n",
    "\n",
    "james = players[players.fullname == 'James, LeBron']\n",
    "james_group = players.Group == james.Group.iloc[0]\n",
    "\n",
    "distances = calculate_distance(normalized_stat[james_group], normalized_stat.loc[james.index].iloc[0])\n",
    "distances.name = 'Distance'\n",
    "pd.concat([players[james_group], distances], axis=1).sort_values(by='Distance')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 以下是分步骤执行讲解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PTS/48</th>\n",
       "      <th>FTA/G</th>\n",
       "      <th>FTM/G</th>\n",
       "      <th>MPG</th>\n",
       "      <th>FGA/G</th>\n",
       "      <th>FGM/G</th>\n",
       "      <th>PTS</th>\n",
       "      <th>GS</th>\n",
       "      <th>FG%</th>\n",
       "      <th>GP</th>\n",
       "      <th>HIGH</th>\n",
       "      <th>FT%</th>\n",
       "      <th>3FGM/G</th>\n",
       "      <th>PPG</th>\n",
       "      <th>PPS</th>\n",
       "      <th>3FG%</th>\n",
       "      <th>3FGA/G</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.140711</td>\n",
       "      <td>0.103359</td>\n",
       "      <td>-0.026436</td>\n",
       "      <td>-0.825379</td>\n",
       "      <td>-0.560919</td>\n",
       "      <td>-0.602876</td>\n",
       "      <td>-0.406933</td>\n",
       "      <td>-1.180173</td>\n",
       "      <td>-0.160045</td>\n",
       "      <td>0.284093</td>\n",
       "      <td>-0.507897</td>\n",
       "      <td>-0.510498</td>\n",
       "      <td>-0.153614</td>\n",
       "      <td>-0.465705</td>\n",
       "      <td>0.488680</td>\n",
       "      <td>0.238151</td>\n",
       "      <td>-0.094572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.026597</td>\n",
       "      <td>-0.697441</td>\n",
       "      <td>-0.552070</td>\n",
       "      <td>-0.963047</td>\n",
       "      <td>-0.612147</td>\n",
       "      <td>-0.492511</td>\n",
       "      <td>-0.439084</td>\n",
       "      <td>-0.969681</td>\n",
       "      <td>0.550083</td>\n",
       "      <td>0.973664</td>\n",
       "      <td>-0.748551</td>\n",
       "      <td>1.020793</td>\n",
       "      <td>-0.695067</td>\n",
       "      <td>-0.585632</td>\n",
       "      <td>-0.047005</td>\n",
       "      <td>-0.076633</td>\n",
       "      <td>-0.561217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.104803</td>\n",
       "      <td>-0.327841</td>\n",
       "      <td>-0.401889</td>\n",
       "      <td>-0.963047</td>\n",
       "      <td>-1.354955</td>\n",
       "      <td>-1.099521</td>\n",
       "      <td>-1.202666</td>\n",
       "      <td>-0.689025</td>\n",
       "      <td>2.201129</td>\n",
       "      <td>-1.646706</td>\n",
       "      <td>-0.989205</td>\n",
       "      <td>-0.664534</td>\n",
       "      <td>-0.965793</td>\n",
       "      <td>-1.065336</td>\n",
       "      <td>2.631419</td>\n",
       "      <td>0.197092</td>\n",
       "      <td>-1.079712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.216342</td>\n",
       "      <td>-0.759041</td>\n",
       "      <td>-0.777342</td>\n",
       "      <td>-1.268976</td>\n",
       "      <td>-1.073200</td>\n",
       "      <td>-1.154704</td>\n",
       "      <td>-1.237496</td>\n",
       "      <td>-0.759189</td>\n",
       "      <td>-0.781406</td>\n",
       "      <td>-0.681306</td>\n",
       "      <td>-1.590840</td>\n",
       "      <td>-0.628290</td>\n",
       "      <td>-1.101156</td>\n",
       "      <td>-1.205250</td>\n",
       "      <td>-1.577533</td>\n",
       "      <td>-1.910592</td>\n",
       "      <td>-1.183411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.683662</td>\n",
       "      <td>1.520159</td>\n",
       "      <td>1.775737</td>\n",
       "      <td>1.713834</td>\n",
       "      <td>1.974873</td>\n",
       "      <td>2.045894</td>\n",
       "      <td>2.301776</td>\n",
       "      <td>1.240482</td>\n",
       "      <td>0.212772</td>\n",
       "      <td>0.422007</td>\n",
       "      <td>3.703547</td>\n",
       "      <td>1.111402</td>\n",
       "      <td>1.741469</td>\n",
       "      <td>2.212644</td>\n",
       "      <td>0.871312</td>\n",
       "      <td>0.929307</td>\n",
       "      <td>1.409063</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     PTS/48     FTA/G     FTM/G       MPG     FGA/G     FGM/G       PTS  \\\n",
       "0  0.140711  0.103359 -0.026436 -0.825379 -0.560919 -0.602876 -0.406933   \n",
       "1 -0.026597 -0.697441 -0.552070 -0.963047 -0.612147 -0.492511 -0.439084   \n",
       "2 -1.104803 -0.327841 -0.401889 -0.963047 -1.354955 -1.099521 -1.202666   \n",
       "3 -1.216342 -0.759041 -0.777342 -1.268976 -1.073200 -1.154704 -1.237496   \n",
       "4  1.683662  1.520159  1.775737  1.713834  1.974873  2.045894  2.301776   \n",
       "\n",
       "         GS       FG%        GP      HIGH       FT%    3FGM/G       PPG  \\\n",
       "0 -1.180173 -0.160045  0.284093 -0.507897 -0.510498 -0.153614 -0.465705   \n",
       "1 -0.969681  0.550083  0.973664 -0.748551  1.020793 -0.695067 -0.585632   \n",
       "2 -0.689025  2.201129 -1.646706 -0.989205 -0.664534 -0.965793 -1.065336   \n",
       "3 -0.759189 -0.781406 -0.681306 -1.590840 -0.628290 -1.101156 -1.205250   \n",
       "4  1.240482  0.212772  0.422007  3.703547  1.111402  1.741469  2.212644   \n",
       "\n",
       "        PPS      3FG%    3FGA/G  \n",
       "0  0.488680  0.238151 -0.094572  \n",
       "1 -0.047005 -0.076633 -0.561217  \n",
       "2  2.631419  0.197092 -1.079712  \n",
       "3 -1.577533 -1.910592 -1.183411  \n",
       "4  0.871312  0.929307  1.409063  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k = 5\n",
    "centers = stat.sample(n=k)\n",
    "centers.reset_index(drop=True, inplace=True)\n",
    "centers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>257</th>\n",
       "      <th>258</th>\n",
       "      <th>259</th>\n",
       "      <th>260</th>\n",
       "      <th>261</th>\n",
       "      <th>262</th>\n",
       "      <th>263</th>\n",
       "      <th>264</th>\n",
       "      <th>265</th>\n",
       "      <th>266</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>11.810445</td>\n",
       "      <td>12.378807</td>\n",
       "      <td>9.327481</td>\n",
       "      <td>9.225911</td>\n",
       "      <td>9.584407</td>\n",
       "      <td>10.156033</td>\n",
       "      <td>8.349430</td>\n",
       "      <td>8.027007</td>\n",
       "      <td>8.654543</td>\n",
       "      <td>8.917280</td>\n",
       "      <td>...</td>\n",
       "      <td>3.897074</td>\n",
       "      <td>4.757560</td>\n",
       "      <td>3.886522</td>\n",
       "      <td>3.462988</td>\n",
       "      <td>5.226107</td>\n",
       "      <td>4.567826</td>\n",
       "      <td>4.531824</td>\n",
       "      <td>4.797437</td>\n",
       "      <td>4.842631</td>\n",
       "      <td>6.164737</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12.650827</td>\n",
       "      <td>13.257922</td>\n",
       "      <td>10.268428</td>\n",
       "      <td>9.654727</td>\n",
       "      <td>10.391494</td>\n",
       "      <td>10.711301</td>\n",
       "      <td>8.701704</td>\n",
       "      <td>8.674197</td>\n",
       "      <td>9.258088</td>\n",
       "      <td>9.435505</td>\n",
       "      <td>...</td>\n",
       "      <td>3.735407</td>\n",
       "      <td>4.750047</td>\n",
       "      <td>4.768493</td>\n",
       "      <td>3.096740</td>\n",
       "      <td>5.457005</td>\n",
       "      <td>4.872139</td>\n",
       "      <td>4.650709</td>\n",
       "      <td>5.213894</td>\n",
       "      <td>5.108138</td>\n",
       "      <td>6.668267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>14.118786</td>\n",
       "      <td>14.648485</td>\n",
       "      <td>11.475401</td>\n",
       "      <td>10.709081</td>\n",
       "      <td>11.188161</td>\n",
       "      <td>12.548523</td>\n",
       "      <td>10.695886</td>\n",
       "      <td>9.808814</td>\n",
       "      <td>11.017960</td>\n",
       "      <td>11.533880</td>\n",
       "      <td>...</td>\n",
       "      <td>4.987771</td>\n",
       "      <td>4.681027</td>\n",
       "      <td>4.559898</td>\n",
       "      <td>5.159650</td>\n",
       "      <td>3.325118</td>\n",
       "      <td>5.262882</td>\n",
       "      <td>2.802413</td>\n",
       "      <td>4.978339</td>\n",
       "      <td>5.413398</td>\n",
       "      <td>5.184707</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14.578849</td>\n",
       "      <td>15.623567</td>\n",
       "      <td>12.401961</td>\n",
       "      <td>11.579640</td>\n",
       "      <td>11.944293</td>\n",
       "      <td>13.506404</td>\n",
       "      <td>10.844188</td>\n",
       "      <td>10.696947</td>\n",
       "      <td>11.785072</td>\n",
       "      <td>12.064890</td>\n",
       "      <td>...</td>\n",
       "      <td>2.092471</td>\n",
       "      <td>3.258761</td>\n",
       "      <td>4.453746</td>\n",
       "      <td>3.530732</td>\n",
       "      <td>4.116214</td>\n",
       "      <td>2.017370</td>\n",
       "      <td>5.590456</td>\n",
       "      <td>3.506902</td>\n",
       "      <td>1.656738</td>\n",
       "      <td>4.954299</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.548965</td>\n",
       "      <td>5.782448</td>\n",
       "      <td>3.555443</td>\n",
       "      <td>5.408165</td>\n",
       "      <td>6.345913</td>\n",
       "      <td>3.376582</td>\n",
       "      <td>4.070384</td>\n",
       "      <td>4.455685</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.566423</td>\n",
       "      <td>...</td>\n",
       "      <td>11.992489</td>\n",
       "      <td>12.149571</td>\n",
       "      <td>11.555714</td>\n",
       "      <td>11.068440</td>\n",
       "      <td>12.629147</td>\n",
       "      <td>12.255728</td>\n",
       "      <td>11.669225</td>\n",
       "      <td>11.978965</td>\n",
       "      <td>12.569615</td>\n",
       "      <td>13.658564</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 267 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         0          1          2          3          4          5    \\\n",
       "0  11.810445  12.378807   9.327481   9.225911   9.584407  10.156033   \n",
       "1  12.650827  13.257922  10.268428   9.654727  10.391494  10.711301   \n",
       "2  14.118786  14.648485  11.475401  10.709081  11.188161  12.548523   \n",
       "3  14.578849  15.623567  12.401961  11.579640  11.944293  13.506404   \n",
       "4   5.548965   5.782448   3.555443   5.408165   6.345913   3.376582   \n",
       "\n",
       "         6          7          8          9      ...            257  \\\n",
       "0   8.349430   8.027007   8.654543   8.917280    ...       3.897074   \n",
       "1   8.701704   8.674197   9.258088   9.435505    ...       3.735407   \n",
       "2  10.695886   9.808814  11.017960  11.533880    ...       4.987771   \n",
       "3  10.844188  10.696947  11.785072  12.064890    ...       2.092471   \n",
       "4   4.070384   4.455685   0.000000   2.566423    ...      11.992489   \n",
       "\n",
       "         258        259        260        261        262        263  \\\n",
       "0   4.757560   3.886522   3.462988   5.226107   4.567826   4.531824   \n",
       "1   4.750047   4.768493   3.096740   5.457005   4.872139   4.650709   \n",
       "2   4.681027   4.559898   5.159650   3.325118   5.262882   2.802413   \n",
       "3   3.258761   4.453746   3.530732   4.116214   2.017370   5.590456   \n",
       "4  12.149571  11.555714  11.068440  12.629147  12.255728  11.669225   \n",
       "\n",
       "         264        265        266  \n",
       "0   4.797437   4.842631   6.164737  \n",
       "1   5.213894   5.108138   6.668267  \n",
       "2   4.978339   5.413398   5.184707  \n",
       "3   3.506902   1.656738   4.954299  \n",
       "4  11.978965  12.569615  13.658564  \n",
       "\n",
       "[5 rows x 267 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distance_matrix = centers.apply(lambda c: calculate_distance(stat, c), axis=1)\n",
    "distance_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      4\n",
       "1      4\n",
       "2      4\n",
       "3      4\n",
       "4      4\n",
       "5      4\n",
       "6      4\n",
       "7      4\n",
       "8      4\n",
       "9      4\n",
       "10     4\n",
       "11     4\n",
       "12     4\n",
       "13     4\n",
       "14     4\n",
       "15     4\n",
       "16     4\n",
       "17     4\n",
       "18     4\n",
       "19     4\n",
       "20     4\n",
       "21     4\n",
       "22     4\n",
       "23     4\n",
       "24     4\n",
       "25     4\n",
       "26     0\n",
       "27     4\n",
       "28     4\n",
       "29     4\n",
       "      ..\n",
       "237    0\n",
       "238    3\n",
       "239    0\n",
       "240    1\n",
       "241    2\n",
       "242    3\n",
       "243    3\n",
       "244    0\n",
       "245    3\n",
       "246    3\n",
       "247    1\n",
       "248    1\n",
       "249    0\n",
       "250    2\n",
       "251    3\n",
       "252    3\n",
       "253    3\n",
       "254    3\n",
       "255    2\n",
       "256    1\n",
       "257    3\n",
       "258    3\n",
       "259    0\n",
       "260    1\n",
       "261    2\n",
       "262    3\n",
       "263    2\n",
       "264    3\n",
       "265    3\n",
       "266    3\n",
       "Length: 267, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "belongings = distance_matrix.idxmin()\n",
    "belongings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PTS/48</th>\n",
       "      <th>FTA/G</th>\n",
       "      <th>FTM/G</th>\n",
       "      <th>MPG</th>\n",
       "      <th>FGA/G</th>\n",
       "      <th>FGM/G</th>\n",
       "      <th>PTS</th>\n",
       "      <th>GS</th>\n",
       "      <th>FG%</th>\n",
       "      <th>GP</th>\n",
       "      <th>HIGH</th>\n",
       "      <th>FT%</th>\n",
       "      <th>3FGM/G</th>\n",
       "      <th>PPG</th>\n",
       "      <th>PPS</th>\n",
       "      <th>3FG%</th>\n",
       "      <th>3FGA/G</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.013683</td>\n",
       "      <td>-0.144922</td>\n",
       "      <td>-0.165153</td>\n",
       "      <td>0.032157</td>\n",
       "      <td>-0.009141</td>\n",
       "      <td>-0.058210</td>\n",
       "      <td>-0.076119</td>\n",
       "      <td>-0.084866</td>\n",
       "      <td>-0.236343</td>\n",
       "      <td>-0.055955</td>\n",
       "      <td>0.001885</td>\n",
       "      <td>-0.060704</td>\n",
       "      <td>0.197710</td>\n",
       "      <td>-0.057255</td>\n",
       "      <td>-0.141641</td>\n",
       "      <td>0.276911</td>\n",
       "      <td>0.206234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.365658</td>\n",
       "      <td>-0.554154</td>\n",
       "      <td>-0.462288</td>\n",
       "      <td>-0.568997</td>\n",
       "      <td>-0.552010</td>\n",
       "      <td>-0.498509</td>\n",
       "      <td>-0.432852</td>\n",
       "      <td>-0.282532</td>\n",
       "      <td>0.108183</td>\n",
       "      <td>0.616886</td>\n",
       "      <td>-0.494818</td>\n",
       "      <td>0.547263</td>\n",
       "      <td>-0.439054</td>\n",
       "      <td>-0.549567</td>\n",
       "      <td>-0.130186</td>\n",
       "      <td>-0.172734</td>\n",
       "      <td>-0.455264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.814338</td>\n",
       "      <td>-0.096841</td>\n",
       "      <td>-0.392503</td>\n",
       "      <td>-0.577767</td>\n",
       "      <td>-1.058792</td>\n",
       "      <td>-0.689099</td>\n",
       "      <td>-0.763941</td>\n",
       "      <td>-0.279005</td>\n",
       "      <td>2.188924</td>\n",
       "      <td>-0.414098</td>\n",
       "      <td>-0.883919</td>\n",
       "      <td>-1.361656</td>\n",
       "      <td>-0.999633</td>\n",
       "      <td>-0.753028</td>\n",
       "      <td>2.023990</td>\n",
       "      <td>-1.298987</td>\n",
       "      <td>-1.092675</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.108123</td>\n",
       "      <td>-0.809642</td>\n",
       "      <td>-0.844387</td>\n",
       "      <td>-1.000196</td>\n",
       "      <td>-0.998188</td>\n",
       "      <td>-1.030543</td>\n",
       "      <td>-1.139800</td>\n",
       "      <td>-0.643920</td>\n",
       "      <td>-0.280513</td>\n",
       "      <td>-0.755189</td>\n",
       "      <td>-1.096640</td>\n",
       "      <td>-0.916944</td>\n",
       "      <td>-0.806258</td>\n",
       "      <td>-1.087465</td>\n",
       "      <td>-1.025449</td>\n",
       "      <td>-1.001433</td>\n",
       "      <td>-0.785281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.362383</td>\n",
       "      <td>1.493377</td>\n",
       "      <td>1.583113</td>\n",
       "      <td>1.287196</td>\n",
       "      <td>1.553911</td>\n",
       "      <td>1.531255</td>\n",
       "      <td>1.609135</td>\n",
       "      <td>1.013212</td>\n",
       "      <td>-0.025738</td>\n",
       "      <td>0.146179</td>\n",
       "      <td>1.464419</td>\n",
       "      <td>0.657372</td>\n",
       "      <td>0.714475</td>\n",
       "      <td>1.636477</td>\n",
       "      <td>0.453744</td>\n",
       "      <td>0.445528</td>\n",
       "      <td>0.726003</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     PTS/48     FTA/G     FTM/G       MPG     FGA/G     FGM/G       PTS  \\\n",
       "0 -0.013683 -0.144922 -0.165153  0.032157 -0.009141 -0.058210 -0.076119   \n",
       "1 -0.365658 -0.554154 -0.462288 -0.568997 -0.552010 -0.498509 -0.432852   \n",
       "2 -0.814338 -0.096841 -0.392503 -0.577767 -1.058792 -0.689099 -0.763941   \n",
       "3 -1.108123 -0.809642 -0.844387 -1.000196 -0.998188 -1.030543 -1.139800   \n",
       "4  1.362383  1.493377  1.583113  1.287196  1.553911  1.531255  1.609135   \n",
       "\n",
       "         GS       FG%        GP      HIGH       FT%    3FGM/G       PPG  \\\n",
       "0 -0.084866 -0.236343 -0.055955  0.001885 -0.060704  0.197710 -0.057255   \n",
       "1 -0.282532  0.108183  0.616886 -0.494818  0.547263 -0.439054 -0.549567   \n",
       "2 -0.279005  2.188924 -0.414098 -0.883919 -1.361656 -0.999633 -0.753028   \n",
       "3 -0.643920 -0.280513 -0.755189 -1.096640 -0.916944 -0.806258 -1.087465   \n",
       "4  1.013212 -0.025738  0.146179  1.464419  0.657372  0.714475  1.636477   \n",
       "\n",
       "        PPS      3FG%    3FGA/G  \n",
       "0 -0.141641  0.276911  0.206234  \n",
       "1 -0.130186 -0.172734 -0.455264  \n",
       "2  2.023990 -1.298987 -1.092675  \n",
       "3 -1.025449 -1.001433 -0.785281  \n",
       "4  0.453744  0.445528  0.726003  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "centers = stat.groupby(belongings).mean()\n",
    "centers"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
